<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Article extends Controller
{
	public $req;
	function __construct() {
		parent::__construct();
		$this->req = Request::instance();

        // 判断当前控制器, 使相应导航高亮
		$con = __CLASS__;
        $arr = explode('\\', $con);
        $this->assign('_class', strtolower($arr[3]));
	}

	//获取文章分类列表
	public function articleCategoryList()
	{

		$page = input('post.page');
        $limit = input('post.limit');
		if ($this->req->isPost()) {
			$list = model('ArticleCategorys')->getAarticleCategoryList($page,$limit);
			$count_list = count(model('ArticleCategorys')->getAarticleCategoryList($page,$limit));
			$reData['code']  = 0;
	        $reData['msg']   = '';
	        // $reData['count'] = $this->pubArtCou;	// 已发布文章数量
	        $reData['count'] = $count_list;	// 所有 文章数量
	        $reData['data']  = $list;
			echo json_encode($reData);
		}else {
			return view('classify/classifylist');
		}

	}

	//添加文章分类post过来的数据
	public function postArticleCategory()
	{
		$category_info['name'] = input('post.category_name');
		$count = model('ArticleCategorys')->createArticleCategory($category_info);
		if ($count) {
			$data['code'] = 200;
            $data['msg'] = '添加成功';
            return json($data);
		}else{
			$data['code'] = 400;
            $data['msg'] = '添加失败';
            return json($data);
		}
	}

	//修改分类内容
	public function updateCategoryInfo()
	{
		$data = input('post.');
		$id = $data['id'];
		$update_info[$data['field']] = $data['value'];
		$count = model('ArticleCategorys')->updateCategoryInfo($id,$update_info);
		if ($count) {
			$data['code'] = 200;
            $data['msg'] = '编辑成功';
            return json($data);
		}else{
			$data['code'] = 400;
            $data['msg'] = '编辑失败';
            return json($data);
		}
	}

	//栏目软删除、恢复操作
	public function softDelete()
	{
		$id = input('post.id');
		if (input('post.key') == 'del') {
			$info = model('ArticleCategorys')->get($id);
			$count = $info->delete();
			
			if ($count) {
				$data['code'] = 200;
	            $data['msg'] = '软删除成功';
	            return json($data);
			}else{
				$data['code'] = 400;
	            $data['msg'] = '软删除失败';
	            return json($data);
			}
		}
		if (input('post.key') == 'recovery') {
			$info = model('ArticleCategorys')->onlyTrashed()->find($id);
			if (!$info) {
				$data['code'] = 400;
	            $data['msg'] = '数据并未删除';
	            return json($data);
			}
			$count = $info->restore();
			if ($count) {
				$data['code'] = 200;
	            $data['msg'] = '恢复成功';
	            return json($data);
			}else{
				$data['code'] = 400;
	            $data['msg'] = '恢复失败';
	            return json($data);
			}
		}
		
	}

	//文章列表
	public function getArticleList()
	{
		
		$page = input('post.page');
        $limit = input('post.limit');
		if ($this->req->isPost()) {
			// $list = model('Articles')->page($page,$limit)->select();
			$list = Db::table('articles')
			->field(['a.*','w.name'])
			->page($page,$limit)
			->alias('a')
			->join('article_categorys w','a.cid = w.id','LEFT')
			->select();
			$count_list = count(model('Articles')->select());
			$reData['code']  = 0;
	        $reData['msg']   = '';
	        // $reData['count'] = $this->pubArtCou;	// 已发布文章数量
	        $reData['count'] = $count_list;	// 所有 文章数量
	        $reData['data']  = $list;
			echo json_encode($reData);
		}else {
			return view('article/articleslist');
		}
	}

	//添加文章
	public function getAddArticle()
	{
		$category_list = model('ArticleCategorys')->select();

		return view('article/addarticles',['category_list'=>$category_list]);
	}

	//保存文章
	public function postAddArticle()
	{
		$data = input('post.');
		$article_info['title'] = $data['title'];
		$article_info['auth'] = $data['auth'];
		$article_info['main_img'] = $data['file'];
		$article_info['cid'] = $data['category_id'];
		$article_info['content'] = $data['content'];
		$count = model('Articles')->createArticle($article_info);
		if ($count) {
			$data['code'] = 200;
            $data['msg'] = '添加文章成功';
            return json($data);
		}else{
			$data['code'] = 400;
            $data['msg'] = '添加文章失败';
            return json($data);
		}
	}

	//预览详情页
	public function getArticleDetail($id)
	{
		$article_info = model('Articles')->getArticleDetail($id);

		return view('article/xiangqing',['article_info'=>$article_info]);
	}

	//编辑文章
	public function editArticle($id)
	{
		$article_info = model('Articles')->getArticleDetail($id);
		$category_list = model('ArticleCategorys')->select();
		return view('article/editarticle',['article_info'=>$article_info,'category_list'=>$category_list]);
	}

	//保存编辑后的文章
	public function postEditArticleInfo($id)
	{
		$data = input('post.');
		$edit_article_info['title'] = $data['title'];
		$edit_article_info['auth'] = $data['auth'];
		$edit_article_info['main_img'] = $data['file'];
		$edit_article_info['cid'] = $data['category_id'];
		$edit_article_info['content'] = $data['content'];
		$count = model('Articles')->updateAarticleInfo($id,$edit_article_info);
		if ($count) {
			$data['code'] = 200;
            $data['msg'] = '修改文章成功';
            return json($data);
		}else{
			$data['code'] = 400;
            $data['msg'] = '修改文章失败';
            return json($data);
		}
	}

	//修改文章状态
	public function editStatus()
	{
		$data = input('post.');
		$status = model('Articles')->where('id',$data['id'])->update(['status' => $data['status']]);
		if ($status) {
			$data['code'] = 200;
            $data['msg'] = '修改成功';
            return json($data);
		}else{
			$data['code'] = 400;
            $data['msg'] = '修改失败';
            return json($data);
		}
	}


}
